package bank
{
	import core.Control;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.ui.Mouse;
	
	import utils.ColorTool;
	import utils.DisplayTool;
	import utils.EnterFrameTimer;
	
	import view.components.CountNumber;
	
	public class Chest extends Sprite
	{
		private var bgClose:DisplayObject;
		private var bgOpen:DisplayObject;
		private var board:ChestBoard;
		private var gold:DisplayObject;
		private var bgType:int = -1;
		private var chestType:int = -1;
		private var _state:int;
		private var takeAnimate:MovieClip;
		private var _info:String;
		private var _level:String;
		private var bankLandControl:BankLandControl;
		private var chestHint:ChestHint;
		private var hintTimer:EnterFrameTimer;
		private var count:CountNumber;
		
		
		public var chestData:XML;
		private var leftTimer:EnterFrameTimer;
		private var leftTotal:uint;
		private var isShowHint:Boolean;
		
		public function Chest(bgType:int,chestType:int,bankLandControl:BankLandControl)
		{
			this.bankLandControl = bankLandControl;
			this.mouseChildren = false;
			this.bgType = bgType;
			this.chestType = chestType;
			this.buttonMode = true;
			initBg();
			initGold();
			initBoard();
			initChestType();
			initTakeAnimate();
			initChestHint();
			initTimer();
		
			addListener();
			
		}
		
		private function initTimer():void
		{
			hintTimer = new EnterFrameTimer(2000);
			hintTimer.addEventListener(TimerEvent.TIMER,onHintTimer);
			
			leftTimer = new EnterFrameTimer(1000);
			leftTimer.addEventListener(TimerEvent.TIMER,onLeftTimer);
		}
		
		private function onLeftTimer(event:TimerEvent):void
		{
			leftTotal--;
			if (leftTotal <= 0)
			{
				leftTimer.stop();
				if (_state == ChestTool.CHEST_STATE_STORE)
				{
					state = ChestTool.CHEST_STATE_HAVEST;
				}
				else if (_state == ChestTool.CHEST_STATE_HAVEST);
				{
					chestData.@leftTime = "";
					state = ChestTool.CHEST_STATE_HAVEST;
				}
			}
			
			
			if (isShowHint)
			{
				if (this.info)
				{
					Control.getInstance().hintControl.showHint(this,_info);
				}
			}
		}
		
		public function set leftTime(v:int):void
		{
			if (v != 0)
			{
				leftTotal = v;
				leftTimer.start();
			}
			
		}
		
		public function getLeftTime():String
		{
			var hour:int = leftTotal / 3600;
			var minute:int = (leftTotal % 3600) / 60;
			var second:int = leftTotal % 60;
			
			return hour + "小时" + minute + "分" + second + "秒";
		}
		
		private function onHintTimer(event:TimerEvent):void
		{
			hintTimer.reset();
			DisplayTool.removeChild(chestHint.parent,chestHint);
		}
		
		private function initChestHint():void
		{
			chestHint = new ChestHint();
			chestHint.x = 80;
			chestHint.y = 10;
		}
		
		public function get level():String
		{
			return _level;
		}

		public function set level(value:String):void
		{
			_level = value;
		}

		public function get info():String
		{
			switch (_state)
			{
				case ChestTool.CHEST_STATE_INVALID:
				case ChestTool.CHEST_STATE_ACTIVATE:
				case ChestTool.CHEST_STATE_EMPTY:
					
					_info = "<font color='#EB4600' size='14'><b>" +  chestData.@name + "</b></font>" +
					"\n" + 
					"<font color='#333333'>" + chestData.@mouseOnDesc + "</font>";
					break;
				case ChestTool.CHEST_STATE_STORE:
					
					_info = "<font color='#EB4600' size='14'><b>" +  chestData.@name + "</b></font>" +
					"\n" +
					"<font size = '12' color='#FF0000'>距离收获还剩：" + getLeftTime() + "</font>\n" + 
					"<font color='#333333'>" + chestData.@mouseOnDesc + "</font>";
					
					break;
				case ChestTool.CHEST_STATE_HAVEST:
					if (bankLandControl.isSelfBank())
					{
						_info = "<font color='#EB4600' size='14'><b>" +  chestData.@name + "</b></font>" +
							"\n" +
							"<font size = '12' color='#FF0000'>已到期，点击收金币了！</font>\n" + 
							"<font color='#333333'>" + chestData.@mouseOnDesc + "</font>";
					}
					else
					{
						if (String(chestData.@leftTime) == "")
						{
							_info = "<font color='#EB4600' size='14'><b>" +  chestData.@name + "</b></font>" +
								"\n" +
								"<font size = '12' color='#FF0000'>到期了，乘主人不在快取吧!</font>" + 
								"<font color='#333333'>\n" + chestData.@mouseOnDesc + "</font>";
						}
						else
						{
							_info = "<font color='#EB4600' size='14'><b>" +  chestData.@name + "</b></font>" +
								"\n" +
								"<font size = '12' color='#FF0000'>距离下次可取还剩：" + getLeftTime() + "</font>\n" + 
								"<font color='#333333'>" + chestData.@mouseOnDesc + "</font>";
							
						}
					}
					break;
				
			}
			return _info;
		}

		public function set info(value:String):void
		{
			_info = value;
		}

		private function initTakeAnimate():void
		{
			takeAnimate = (new ChestTool.TakeAnimate()) as MovieClip;
			
			
			((takeAnimate as DisplayObjectContainer).getChildAt(0) as Loader).contentLoaderInfo.addEventListener(Event.COMPLETE,onCO);
			
			function onCO(event:Event):void
			{
				event.target.removeEventListener(Event.COMPLETE,onCO);
				takeAnimate = (((takeAnimate as DisplayObjectContainer).getChildAt(0) as Loader).content as MovieClip)
				takeAnimate.addEventListener(Event.COMPLETE,onComplete);
			}
			
		}
		
		private function onComplete(event:Event):void
		{
			DisplayTool.removeChild(this,takeAnimate);
			Mouse.show();
		}
		
		private function addListener():void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,onOut);
			this.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
		}
		
		private function onDown(event:MouseEvent):void
		{
			this.mouseEnabled = false;
			switch (_state)
			{
				case ChestTool.CHEST_STATE_ACTIVATE:
					bankLandControl.openChest(chestType,this);
			
					break;
				case ChestTool.CHEST_STATE_EMPTY:
					bankLandControl.saveMoney(chestType,this);
		
					break;
				case ChestTool.CHEST_STATE_HAVEST:
					bankLandControl.havestMoney(chestType,this);
	
					break;
				case ChestTool.CHEST_STATE_STORE:
					bankLandControl.forceGet(chestType,this);
				
					break;
			}
			
			if (bgType == 0)
			{
				takeAnimate.x = 40;
				takeAnimate.y = 15;
			}
			else if (bgType == 1)
			{
				takeAnimate.x = 20;
				takeAnimate.y = 50;
			}
			
			if (_state != ChestTool.CHEST_STATE_HAVEST)
			{
				return;
			}
			
			
			
			
			this.mouseEnabled = false;
		}
		
		public function playTakeAnimate():void
		{
			Mouse.hide();
			takeAnimate.gotoAndPlay(1);
			addChild(takeAnimate);
		}
		
		override public function get height():Number
		{
			return bgClose.height;
		}
		
		private function onOver(event:MouseEvent):void
		{
			
			ChestTool.glowChest(bgClose);
			ChestTool.glowChest(bgOpen);
			if (this.info)
			{
				
				Control.getInstance().hintControl.showHint(this,_info);
			}
			isShowHint = true;
		}
		
		private function drawTest():void
		{
			this.graphics.lineStyle(1,0xFF0000);
			this.graphics.drawRect(0,0,this.width,this.height);
		}
		
		private function onOut(event:MouseEvent):void
		{
		
			bgClose.filters = null;
			bgOpen.filters = null;
			if (this._info)
			{
				Control.getInstance().hintControl.clear();
			}
			isShowHint = false;
		}
		
		private function initBoard():void
		{
			board = new ChestBoard();
			
		}
		
		public function updateHint(content:String):void
		{
			if (content == "")
			{
				return;
			}
			
			hintTimer.start();
			chestHint.update(content);
			//chestHint.visible = true;
			
			this.addChild(chestHint);
		}
		
		
		
		override public function get width() : Number
		{
			return (bgClose.parent)?bgClose.width:bgOpen.width;
		}
		
		public function set state(v:int):void
		{
			_state = v;
			DisplayTool.removeChild(this,bgClose);
			DisplayTool.removeChild(this,bgOpen);
			DisplayTool.removeChild(this,board);
			DisplayTool.removeChild(this,gold);
			DisplayTool.removeChild(this,takeAnimate);
				
			board.x = 40;
			board.y = 35;
			board.update(v,_level);
			this.mouseEnabled = true;
			
			switch (_state)
			{
				case ChestTool.CHEST_STATE_INVALID:
					addChild(bgClose);
					ColorTool.saturation(bgClose);
					this.mouseEnabled = false;
					
					break;
				case ChestTool.CHEST_STATE_ACTIVATE:
					addChild(bgClose);
					ColorTool.saturation(bgClose);
					board.y = bgClose.height - board.height;
					break;
				case ChestTool.CHEST_STATE_EMPTY:
					addChild(bgOpen);
					ChestTool.setChestColor(bgOpen,chestType);
					break;
				case ChestTool.CHEST_STATE_STORE:
					addChild(bgClose);
					ChestTool.setChestColor(bgClose,chestType);
					break;
				case ChestTool.CHEST_STATE_HAVEST:
					addChild(bgOpen);
					addChild(gold);
					ChestTool.setChestColor(bgOpen,chestType);
					break;
			}
			addChild(board);
			//addChild(chestHint);
			
			if (chestHint.parent == this)
			{
				addChild(chestHint);
			}
			
			if (count && count.parent == this)
			{
				addChild(count);
			}
		}
		
		public function updateMoneyChange(v:int):void
		{
			if (v<0)
			{
				count = new CountNumber(CountNumber.GREEN,24);
				
			}
			else
			{
				count = new CountNumber(CountNumber.GOLD,24);
				
			}
			count.addEventListener(Event.COMPLETE,onMoneyChangeComplete);
			count.x = -10;
			addChild(count);
			
			if (v<0)
			{
				count.show("$" + v,10,-20);
			}
			else
			{
				count.show("+$" + v,10,-20);
			}
		}
		
		private function onMoneyChangeComplete(event:Event):void
		{
			event.target.destroy();
			DisplayTool.removeChild(this,event.target as DisplayObject);
		}
		
		public function get state():int
		{
			return _state;
		}
		
		private function initChestType():void
		{
			ChestTool.setChestColor(bgClose,chestType);
			ChestTool.setChestColor(bgOpen,chestType);
		}
		
		private function initGold():void
		{
			if (bgType == 0)
			{
				gold = new ChestTool.Chest1Gold();
				gold.x = - 28;
				gold.y = - 34;
			}
			else if (bgType == 1)
			{
				gold = new ChestTool.Chest2Gold();
				gold.x = - 27;
				gold.y = - 18;
			}
		}
		
		private function initBg():void
		{
			if (bgType == 0)
			{
				bgClose = new Bitmap(ChestTool.chest1close.bitmapData);
				bgOpen = new Bitmap(ChestTool.chest1open.bitmapData);
				bgOpen.y = bgClose.height - bgOpen.height;
			}
			else if (bgType == 1)
			{
				bgClose = new Bitmap(ChestTool.chest2close.bitmapData);
				bgOpen = new Bitmap(ChestTool.chest2open.bitmapData);
				bgOpen.x = bgClose.width - bgOpen.width;
			}
			
		}
		
		public function clear(isClearHint:Boolean):void
		{
			if (isClearHint)
			{
				hintTimer.reset();
				DisplayTool.removeChild(chestHint.parent,chestHint);
				DisplayTool.removeChild(this,takeAnimate);
			}
			this.mouseEnabled = true;
			state = ChestTool.CHEST_STATE_INVALID;
			chestData = null;
			leftTimer.reset();
		}
	}
}